﻿using OestsCommon.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace OestsDataBase
{
    public class ClassDAL
    {
        const string TABLE_NAME = "T_Class";

        private static ClassModel ReadData(DataRow dr)
        {
            ClassModel cm = new ClassModel();
            cm.Id = Int32.Parse(dr["id"].ToString());
            cm.MajorId = dr["major_id"].Equals(DBNull.Value) ? 0 : int.Parse(dr["major_id"].ToString());
            cm.Name = dr["name"].ToString();
            return cm;
        }

        /// <summary>
        /// 返回 Identity
        /// </summary>
        /// <param name="choice"></param>
        /// <returns></returns>
        public static int AddClass(ClassModel cls)
        {
            //DBManager.Checkident(TABLE_NAME);
            string strSql = string.Format("insert into T_Class (name,major_id) values('{0}',{1});SELECT @@Identity", cls.Name, cls.MajorId);
            object obj = SqlHelper.ExecuteScalar(strSql);
            return Int32.Parse(obj.ToString());
        }

        public static List<ClassModel> GetData()
        {
            List<ClassModel> list = new List<ClassModel>();
            string strSql = "select * from T_Class";
            DataTable dt = SqlHelper.ExecuteDataTable(strSql);
            if (dt == null) { return null; }
            foreach (DataRow dr in dt.Rows)
            {
                ClassModel scm = ReadData(dr);
                list.Add(scm);
            }
            return list;
        }

        public static bool Delete(int clsID)
        {
            string strSql = string.Format("delete from T_Class where id = {0}", clsID);
            int n = SqlHelper.ExecuteNonQuery(strSql);
            if (n != 0)
            {
                //string strSql1 = string.Format("update  T_StudentClass set class_id={0} where class_id = {1}",DBNull.Value, clsID);
                //SqlHelper.ExecuteNonQuery(strSql1);
                StudentClassDAL.DeleteByClassID(clsID);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除一个列表的数据
        /// </summary>
        /// <param name="delIdList"></param>
        /// <returns></returns>
        public static void Delete(List<int> delIdList)
        {
            if (delIdList == null || delIdList.Count == 0) return;
            //删除班级表
            string idStr = string.Join(",", delIdList);
            string strSql = string.Format("delete from T_Class where id in ({0})", idStr);
            int n = SqlHelper.ExecuteNonQuery(strSql);
            //删除 学生班级表
            StudentClassDAL.DeleteByClassID(delIdList);
        }


        public static bool Modify(ClassModel cls)
        {
            string strSql = string.Format("update T_Class set major_id={0},name='{1}' where id = @id", cls.MajorId, cls.Name);
            try
            {
                int n = SqlHelper.ExecuteNonQuery(strSql, new SqlParameter("@id", cls.Id));
                if (n != 0)
                    return true;
            }
            catch (Exception)
            {
                return false;
            }
            return false;
        }


        public static bool CheckExist(string clsName)
        {
            string strSql = string.Format("select Count(*) from T_Class where name='{0}'", clsName);
            object n = SqlHelper.ExecuteScalar(strSql);
            return (int)n > 0;
        }

        public static bool CheckExist(ClassModel cls)
        {
            string strSql = string.Format("select * from T_Class where id={0} and major_id={1}  ", cls.Id, cls.MajorId);
            DataTable dt = SqlHelper.ExecuteDataTable(strSql);
            if (dt != null && dt.Rows.Count != 0)
            {
                ClassModel existClass = ReadData(dt.Rows[0]);
                if (existClass.Name == cls.Name && existClass.MajorId == cls.MajorId)
                    return true;
            }
            return false;
        }

        public static bool CheckExist(string name, int majorID)
        {
            string strSql = "select Count(*) from T_Class where name = @name and major_id = @major_id";
            object obj = SqlHelper.ExecuteScalar(strSql, new SqlParameter("@name", name), new SqlParameter("@major_id", majorID));
            return ((int)obj > 0); ;
        }
    }
}
